package org.smr.dev;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TableStructUtils {

	
	
	public static String tableName2BeanName(String tableName){
		
		tableName = tableName.substring(3);
		tableName = getFieldName(tableName);
		tableName = tableName.replace(tableName.charAt(0),((char)(tableName.charAt(0)-32)));
		return tableName;
		
	}
	
	/**
	 * 获取表结构
	 * 
	 * @param tableName
	 * @return
	 */
	public static List<ColumnModel> getTableStructure(String tableName) {
		List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
		try {
			// TODO 表相关
			// ResultSet tableSet = metaData.getTables(null, "%",tableName,new
			// String[]{"TABLE"});
			// TODO 字段相关
			ResultSet columnSet = JdbcConnectionUtils.getConnection().getMetaData().getColumns(null, "%", tableName,
					"%");
			ColumnModel columnModel = null;
			while (columnSet.next()) {
				columnModel = new ColumnModel();
				columnModel.setColumnName(columnSet.getString("COLUMN_NAME"));
				columnModel.setColumnSize(columnSet.getInt("COLUMN_SIZE"));
				columnModel.setDataType(columnSet.getString("DATA_TYPE"));
				columnModel.setRemarks(columnSet.getString("REMARKS"));
				columnModel.setTypeName(columnSet.getString("TYPE_NAME"));
				String columnClassName = ColumnTypeEnum.getColumnTypeEnumByDBType(columnModel.getTypeName());
				String fieldName = getFieldName(columnModel.getColumnName());
				String fieldType = Class.forName(columnClassName).getSimpleName();
				columnModel.setFieldName(fieldName);
				columnModel.setColumnClassName(columnClassName);
				columnModel.setFieldType(fieldType);
				columnModelList.add(columnModel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return columnModelList;
	}

	/**
	 * 将数据库字段转换成bean属性
	 * 
	 * @param columnName
	 * @return
	 */
	private static String getFieldName(String columnName) {

		char[] columnCharArr = columnName.toLowerCase().toCharArray();
		StringBuffer sb = new StringBuffer();
		int ad = -1;
		for (int i = 0; i < columnCharArr.length; i++) {
			char cur = columnCharArr[i];
			if (cur == '_') {
				ad = i;
			} else {
				if ((ad + 1) == i && ad != -1) {
					sb.append(Character.toUpperCase(cur));
				} else {
					sb.append(cur);
				}
				ad = -1;
			}
		}
		return sb.toString();
	}

	public static List<String> getTables() throws SQLException {
		DatabaseMetaData databaseMetaData = (DatabaseMetaData) JdbcConnectionUtils.getConnection().getMetaData();

		ResultSet tableRet = databaseMetaData.getTables(null, "%", "%", new String[] { "TABLE" });
		List<String> list = new ArrayList<String>();
		while (tableRet.next()){
			list.add(tableRet.getString("TABLE_NAME"));
		}
		return list;
	}

	public static void main(String[] args) {
		// System.out.println(getTableStructure("t_sys_url"));
		try {
			getTables();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
